package cn.edu.sdjzu.xg.bysj.service;
//201902104061徐中堂
import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectCategoryDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectCategory;
import util.JdbcHelper;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;


public class GraduateProjectCategoryService {
    //获取 GraduateProjectCategoryDao实例
    private GraduateProjectCategoryDao graduateProjectCategoryDao= GraduateProjectCategoryDao.getInstance();
    //新建 GraduateProjectCategory对象
    private static GraduateProjectCategoryService graduateProjectCategoryService = new GraduateProjectCategoryService();
    //构造器
    private GraduateProjectCategoryService(){}
    //获取GraduateProjectCategoryService对象
    public static GraduateProjectCategoryService getInstance(){
        return graduateProjectCategoryService;
    }

    public GraduateProjectCategory find(int id) throws SQLException {
        //获取数据库链接
        Connection connection = JdbcHelper.getConn();
        GraduateProjectCategory graduateProjectCategory =null;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            graduateProjectCategory = graduateProjectCategoryDao.find(connection,id);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return graduateProjectCategory;
    }

    public Collection<GraduateProjectCategory> findAll() throws SQLException {
        Connection connection =  JdbcHelper.getConn();
        Collection<GraduateProjectCategory> graduateProjectCategorys =null;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            graduateProjectCategorys = graduateProjectCategoryDao.findAll();
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return  graduateProjectCategorys;
    }
}
